package com.util.file;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BinaryFileReader {
	/**
	 * 파일 구분자
	 */
	public final String ANDROID = "EagleTalk_v";
	public final String ANDROID_TAB = "EagleTalkTab_v";
	public final String IPHONE = "eagletalk_v";
	public final String IPAD = "eagletalkipad_v";
	
	/**
	 * 바이너리 등록 폴더 경로
	 */
	public static final String PATH_NAME = "D:/files/photos/cate_photo";
//	public static final String PATH_NAME = "/eagleTalk_data/files/update_binary/";
	
	
	private List<VersionInfo> versionPaths = new ArrayList<VersionInfo>();
	
	private List<String> android = new ArrayList<String>();
	private List<String> iphone = new ArrayList<String>();
	private List<String> androidTab = new ArrayList<String>();
	private List<String> iPad = new ArrayList<String>();

	/**
	 * update_binary에 버젼별 폴더를 읽어 온다
	 */
	public void readCatergoryList(){
		try{
			File file = new File(PATH_NAME);
			File[] files = file.listFiles();
			
			for(int i=0; i<files.length; i++){
				if(files[i].isDirectory()){
					VersionInfo version= new VersionInfo();
//					version.setVersion(directoryName);
					version.setVersionPath(files[i].getAbsolutePath());
					versionPaths.add(version);
					
				}
			}
		}catch(NullPointerException ne){
			ne.printStackTrace();
		}
		
	}
	
	/**
	 * 폴더 경로를 입력하면 폴더에 대한 바이너리를 읽어온다
	 * 1. 아이폰
	 * 2. 안드로이드 폰
	 * 3. 아이패드
	 * 4. 안드로이드 탭
	 * @param pathName
	 */
	public void readFileNames(String pathName) {
		listClear();
		try{
			File file = new File(pathName);
	
			if (file.isDirectory()) {
				File[] files = file.listFiles(new FileFilter() {
	
					@Override
					public boolean accept(File pathname) {
						// TODO Auto-generated method stub
						if (pathname.getName().endsWith(".apk")) {
							return true;
						}
						if (pathname.getName().endsWith(".plist")) {
							return true;
						}
						return false;
					}
				});
	
				if (files == null || files.length == 0) {
					return;
				}
	
				for (int i = 0; i < files.length; i++) {
					if (files[i].isFile()) {
						String name = files[i].getName();
						if (name.contains(ANDROID)) {
							android.add(name);
						} else if (name.contains(ANDROID_TAB)) {
							androidTab.add(name);
						} else if (name.contains(IPHONE)) {
							iphone.add(name);
						} else if (name.contains(IPAD)) {
							iPad.add(name);
						}
					}
				}
			}
		}catch(NullPointerException ne){
			ne.printStackTrace();
		}
	}
	
	/**
	 * 검증용 로그
	 */
	public void printNames(){
		System.out.println(android);
		System.out.println(iphone);
		System.out.println(androidTab);
		System.out.println(iPad);
	}

	/**
	 * @return android fileNames list
	 */
	public List<String> getAndroid() {
		if (android.size() > 0 ){
			Collections.sort(android);
		}
		return android;
	}

	/**
	 * @return iphone fileNames list
	 */
	public List<String> getIphone() {
		if (iphone.size() > 0 ){
			Collections.sort(iphone);
		}
		return iphone;
	}

	/**
	 * @return androidTab fileNames list
	 */
	public List<String> getAndroidTab() {
		if (androidTab.size() > 0 ){
			Collections.sort(androidTab);
		}
		return androidTab;
	}

	/**
	 * @return iPad fileNames list
	 */
	public List<String> getiPad() {
		if (iPad.size() > 0 ){
			Collections.sort(iPad);
		}
		return iPad;
	}
	
	/**
	 * @return 버젼별 Info
	 */
	public List<VersionInfo> getVersionPaths(){
		return versionPaths;
	}
	
	/**
	 * clear
	 */
	private void listClear(){
		android.clear();
		androidTab.clear();
		iPad.clear();
		iphone.clear();
	}
	
	public class VersionInfo {
		private String version;
		private String versionPath;
		
		public String getVersion() {
			return version;
		}
		public void setVersion(String version) {
			this.version = version;
		}
		public String getVersionPath() {
			return versionPath;
		}
		public void setVersionPath(String versionPath) {
			this.versionPath = versionPath;
		}
	}
}
